////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////

package fxplugins.plugin
{

import mx.core.IMXMLObject;

import fxplugins.utility.IUtility;

/**
 * The <code>Plugin</code> describes a bundle of <code>IPluginExtension</code>,
 * <code>IPluginExtensionPoint</code> and <code>IUtility</code> instances 
 * contributing to an application.
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @productversion 1.0
 */
public class Plugin implements IMXMLObject, IPlugin
{
	//--------------------------------------------------------------------------
	//
	//  Private :: Varibles
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private var pluginInitialized:Boolean = false;
	
	//--------------------------------------------------------------------------
	//
	//  IPlugin API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  uid
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _uid:String;
	
	/**
	 * @copy fxplugins.plugin.IPlugin#uid
	 */
	public function get uid():String
	{
		return _uid;
	}
	
	/**
	 * @private
	 */
	public function set uid(value:String):void
	{
		_uid = value;
	}
	
	//----------------------------------
	//  points
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _points:Array;
	
	[ArrayElementType("fxplugins.plugin.IPluginExtensionPoint")]
	
	/**
	 * @copy fxplugins.plugin.IPlugin#points
	 */
	public function get points():Array
	{
		return _points;
	}
	
	/**
	 * @private
	 */
	public function set points(value:Array):void
	{
		// no new points if _points is null, this is the first time
		if (_points == null || pluginInitialized)
		{
			_points = value;
			return;
		}
			
		// in MXML, if you subclass an MXML component with another MXML component
		// the properties will be set twice, we use this to our advantage
		// by pushing the extension points on the the existing array
		// untill initilized
		for (var i:int = 0; i < value.length; i++)
		{
			_points.push(IPluginExtensionPoint(value[i]));
		}
	}
	
	//----------------------------------
	//  extentions
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _extentions:Array;
	
	[ArrayElementType("fxplugins.plugin.IPluginExtension")]
	
	/**
	 * @copy fxplugins.plugin.IPlugin#extentions
	 */
	public function get extentions():Array
	{
		return _extentions;
	}
	
	/**
	 * @private
	 */
	public function set extentions(value:Array):void
	{
		// no new extensions if _extentions is null, this is the first time
		if (_extentions == null || pluginInitialized)
		{
			_extentions = value;
			return;
		}
		
		// in MXML, if you subclass an MXML component with another MXML component
		// the properties will be set twice, we use this to our advantage
		// by pushing the extensions on the the existing array
		// untill initilized
		for (var i:int = 0; i < value.length; i++)
		{
			_extentions.push(IPluginExtension(value[i]));
		}
	}
	
	//----------------------------------
	//  utilities
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _utilities:Array;
	
	[ArrayElementType("fxplugins.utility.Utility")]
	
	/**
	 * @copy fxplugins.plugin.IPlugin#utilities
	 */
	public function get utilities():Array
	{
		return _utilities;
	}
	
	/**
	 * @private
	 */
	public function set utilities(value:Array):void
	{
		// no new utilities if _utilities is null, this is the first time
		if (_utilities == null || pluginInitialized)
		{
			_utilities = value;
			return;
		}
		
		// in MXML, if you subclass an MXML component with another MXML component
		// the properties will be set twice, we use this to our advantage
		// by pushing the utilities on the the existing array
		// untill initilized
		for (var i:int = 0; i < value.length; i++)
		{
			_utilities.push(IUtility(value[i]));
		}
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * @private
	 */
	public function Plugin()
	{
	}
	
	//--------------------------------------------------------------------------
	//
	//  IMXMLObject API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	public function initialized(document:Object, id:String):void
	{
		pluginInitialized = true;
	}
}
}